package com.cloud.controller;


import com.cloud.constent.Constant;
import com.cloud.entity.CommonResult;
import com.cloud.entity.Payment;
import com.cloud.openfeign.PaymentOpenFeign;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
/**
 * 默认hystrix
 */
@DefaultProperties(defaultFallback = "defaultFallbackMethod")
public class OrderController {


    /**
     * 远程调用模板
     */
    @Resource
    RestTemplate restTemplate;

    @Resource
    PaymentOpenFeign paymentOpenfeign;

    @RequestMapping(value = "order/payment/select/{id}")
    @HystrixCommand(fallbackMethod = "orderErrorMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")}
    )
    public CommonResult<Payment> select(@PathVariable Long id) {
        return paymentOpenfeign.select(id);
    }

    @RequestMapping(value = "order/payment/select/default/{id}")
    @HystrixCommand
    public CommonResult<Payment> HystrixCommand(@PathVariable Long id) {
        int a = 1/0;
        return paymentOpenfeign.select(id);
    }

    public CommonResult<Payment> orderErrorMethod(@PathVariable Long id) {
        return new CommonResult<>(Constant.ERRORCODE, "BOOM!");
    }

    public CommonResult<Payment> defaultFallbackMethod() {
        return new CommonResult<>(Constant.ERRORCODE, "BOOM--default!");
    }
}
